<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ImageController
 *
 * @author simonl
 */
class ImageController extends DooController {
    public function __construct() {
        Doo::conf()->TEMPLATE_COMPILE_ALWAYS = true;
        if(isset(self::session()->username)){
            $this->data['username'] = self::session()->username;
        }else{
            $this->data['username']  = '';
            
        }
        if(isset(self::session()->canAdd)&&self::session()->canAdd==true){
              $this->data['canAdd']  = true;
        }else{
             $this->data['canAdd']  = false;
        }
         $this->data['rootUrl']  = Doo::conf()->APP_URL;
       
    }


    public function index(){
       
        
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
      $this->render('index', $this->data);
    }
    
    public function login_page(){
          $this->data['rootUrl'] = Doo::conf()->APP_URL;
      $this->render('login', $this->data);
        
        
    }
    public function signup_page(){
            $this->data['rootUrl'] = Doo::conf()->APP_URL;
      $this->render('signup', $this->data);
        
        
    }
     public function login() {
        $u = Doo::loadModel('user', true);
        $u->username = $_POST['username'];
        $u->password = $_POST['password'];
        $res = $u->getOne();
        if (!$res) {
            echo "<script>alert('username or pasword error');history.go(-1)</script>";
            exit;
        } else {
            self::session()->username = $u->username;
            self::session()->password = $u->password;
            $this->data['rootUrl'] = Doo::conf()->APP_URL;
            $this->data['username'] = $u->username;
            $this->data['time'] = date('Y/m/d H:i');
     $this->render('index', $this->data);
        }
    }
      public function logout() {
        Doo::session()->destroy();
        $rootUrl = Doo::conf()->APP_URL;
        echo "<script>alert('logout success');location.href='{$rootUrl}image/index'</script>";
    }
      public function signup() {
        $u = Doo::loadModel('user', true);
        $u->username = $_POST['username'];
        if($u->count()>0){
            
               echo "<script>alert('username is repeat');history.go(-1)</script>";
               exit;
        }
        $u->password = $_POST['password1'];
        $u->insert();
        self::session()->username = $u->username;
        self::session()->password = $u->password;
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
        $this->data['username'] = $u->username;
        $this->data['time'] = date('Y/m/d H:i');

        $this->render('index', $this->data);
    }
    
     public static function session($namespace = 'user') {
        return Doo::session($namespace);
    }

    
    
    
    //typePage
     public function typePage(){
       
        
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
      $this->render('type', $this->data);
    }
    //联系方式
    public function contactPage(){
         $this->data['rootUrl'] = Doo::conf()->APP_URL;
      $this->render('contact', $this->data);


    }
    public function artistPage(){
            $this->data['rootUrl'] = Doo::conf()->APP_URL;
      $this->render('artist', $this->data);


    }


    public function artistProuctPage(){
             $this->data['rootUrl'] = Doo::conf()->APP_URL;
      $this->render('artist_product', $this->data);

    }
    public function modelPage(){
           $this->data['rootUrl'] = Doo::conf()->APP_URL;
      $this->render('model', $this->data);



    }
    public function download(){

        //$folder = Doo::conf()->SITE_PATH . Doo::conf()->PROTECTED_FOLDER . "userfiles/datatemplate/";
       $folder = Doo::conf()->APP_URL.'down/';
        $filename = 'website_model.doc';
        $content = self::ReadFromFile($folder . $filename);
        self::downloadFile($content, $filename);




    }
     /**
     * 读文件内容
     */
    public static function ReadFromFile($filename) {
        $content = @file_get_contents($filename);
        return $content;
    }

    /**
     * 下载文件
     */
    public static function downloadFile($content, $filename) {
        header("Cache-Control:public");
        header("Pragma:public");
        header("Content-type: application/octet-stream");
        header("Accept-Ranges: bytes");
        header("Content-Disposition:attachment; filename=" . $filename);
        header("Content-length:" . strlen($content));
        echo($content);
        exit;
    }
    public function add() {
        
        $image = Doo::loadHelper('DooGdImage', true);
        $image = new DooGdImage('upload/', 'process/', true, false);
        // $image->uploadImage($filename)
        $isimage = $image->checkImageExtension('image');
        if (!$isimage) {
           return array('/error/fomatError','internal');
        }
       // print_r($_FILES);
      //  exit;
        $_FILES['image']['name'] =  iconv('utf-8','GBK', $_FILES['image']['name']) ;
       
         $custom_name = time();
        $imagename = $image->uploadImage('image',$custom_name);
        $thumb_name = $image->createThumb($imagename, 212, 160);
        $image->createThumb($imagename, 772, 434,     $imagename.'_middle' );
        //   $image->createThumb($imagename, 944, 316, $custom_name.'_middle' );
        $p = Doo::loadModel('Photo', true);
        $p->name =   $imagename;
        $p->thumb_name =  iconv('GBK','utf-8', $thumb_name) ;
        $p->createtime = date('Y-m-d H:i:s');
        $p->custom_name = $_POST['title'];
        $p->custom_number = $_POST['number'];
        $p->custom_size = $_POST['size'];
        $p->custom_type = $_POST['type'];
        $p->custom_keyword = $_POST['keyword'];
        $p->custom_id = $_POST['id'];
        $p->insert();
       
           $this->render('index', $this->data);
   //return array("/image/index}",'internal');
        
    }
    public  function info(){
       
        $id = $_GET['id'];
           $p = Doo::loadModel('Photo', true);
        $p->photoid =   $id;
        $info =  $p->getOne();
        $this->data['info'] = $info;
         $this->render('info', $this->data);
        
        
        
        
    }




    public function del(){
         $id = $_GET['id'];
              $p = Doo::loadModel('Photo', true);
              $p->photoid = $id;
              $p->delete();
              echo "<script>alert('delete success');history.go(-1)</script>";
             exit;
        
        
        
    }
    public function editPage(){
           $id = $_GET['id'];
              $p = Doo::loadModel('Photo', true);
              $p->photoid = $id;
              $info = $p->getOne();
              $this->data['info'] = $info;
           
                   $this->data['rootUrl'] = Doo::conf()->APP_URL;
           $this->render('edit', $this->data);
        
    }
    public function update(){
          $id = $_POST['photoid'];
              $p = Doo::loadModel('Photo', true);
              $p->photoid = $id;
              $p= $p->getOne();
                $p->custom_name = $_POST['title'];
        $p->custom_number = $_POST['number'];
        $p->custom_size = $_POST['size'];
        $p->custom_type = $_POST['type'];
        $p->custom_keyword = $_POST['keyword'];
        $p->custom_id = $_POST['id'];
        $p->update();
            $this->render('index', $this->data);
        
        
    }

    public function getListByType() {
        Doo::loadHelper('DooPager');
        $typename = array('','figure','landscape','people','still lifes','others');
        $typeid = $this->params['typeid'];
         
       $image = Doo::loadModel('Photo', true);
       if( $typeid>0){
            $image->custom_type = $typeid;
       }
       
      //搜索关键字
        $opt = array();
        if (!empty($_REQUEST['keyword'])) {
            $keyword = $_REQUEST['keyword'];
            $opt['where']= "custom_name like '%$keyword%'";
            $opt['param'] = array($keyword );
        }
      
        $pager = new DooPager(  Doo::conf()->APP_URL."image/type/{$typeid}/" . 'page',  $image->count($opt));
        if (isset($this->params['pageindex'])&&$this->params['pageindex']>0){
            $pager->paginate(intval($this->params['pageindex']));
        
        }
            
        else{
            
            $pager->paginate(1);
        }
        $opt['desc'] =  'createtime';
         $opt['limit'] = $pager->limit;

        $list = $image->find($opt);
        $this->data['pager'] = $pager->output;

        $this->data['typename'] =   $typename [$typeid];
        $this->data['list'] = $list;
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
        $this->render('list', $this->data);
    }

    public function showJs() {

        $typeid = $this->params['typeid'];
        $imageid = $this->params['imageid'];
        $image = Doo::loadModel('Photo', true);
        $image->custom_type = $typeid;
     $list = $image->find(array('desc' => 'createtime'));

        $this->data['list'] = $list;
        $this->data['imageid'] = $imageid;
        $this->data['rootUrl'] = Doo::conf()->APP_URL;


        $this->render('showjs', $this->data);
    }

    public function addPage() {

        $this->data['rootUrl'] = Doo::conf()->APP_URL;
        $this->render('add', $this->data);
    }

    //put your code here
}

?>
